home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 22 / Amiga Format AFCD22 (Jan 1998, Issue 106).iso / -seriously_amiga- / shareware / hardware / lcdaemon / programmer / sysinfo.cpp < prev   
C/C++ Source or Header  |  1997-11-05  |  6KB  |  307 lines

  1. #include <pragma/exec_lib.h>
  2. #include <pragma/dos_lib.h>
  3. #include <pragma/SysInfo_lib.h>
  4. #pragma header
  5. #include <iostream.h>
  6. #include <Classes/Exec/Libraries.h>
  7. #include <Classes/Dos/Arguments.h>
  8. #include <lcd.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11.  
  12. LibraryBaseErrC SysInfoBase(SYSINFONAME,SYSINFOVERSION);
  13.  
  14. struct SysInfo *sysinfo;
  15.  
  16. class VomitX{};
  17.  
  18. class CustomCharDefs
  19. {
  20.     public:
  21.         CustomCharDefs();
  22.         operator char *();
  23.     private:
  24.         UBYTE chars[48];
  25. //    char * operator();
  26. };
  27.  
  28. CustomCharDefs::CustomCharDefs()
  29. {
  30.     for(register int i=0;i<6;i++){
  31.         for(register int j=0;j<8;j++){
  32.             if(j%2 && j<7){
  33.                 chars[i*8+j]=0xff;
  34.                 for(register int k=0;k<5-i;k++){
  35.                     chars[i*8+j]<<=1;
  36.                 }
  37.             }else{
  38.                 chars[i*8+j]=0x00;
  39.             }
  40.         }
  41.     }
  42. }
  43.  
  44. CustomCharDefs::operator char*()
  45. {
  46.     return chars;
  47. }
  48.  
  49. class LCD
  50. {
  51.     public:
  52.         LCD() throw(VomitX);
  53.         void SetLCDMsg(LONG code,APTR data,LONG ticks,BYTE pri);
  54.         void SetLCDMsg(APTR data);
  55.         int SendLCDMsg();
  56.         int width(){return lcdpar.width;};
  57.         int height(){return lcdpar.height;};
  58.         int WaitLCDMsg(void);
  59.         APTR LCDData();
  60.         ~LCD();
  61.     private:
  62.     struct MsgPort *replyport;
  63.     struct lcdmessage msg;
  64.     struct lcdparams lcdpar;
  65.     BOOL messagepending;
  66. };
  67.  
  68. LCD::LCD()
  69. {
  70.     messagepending=FALSE;
  71.     if(replyport=CreateMsgPort()){
  72.         msg.lcd_Message.mn_Node.ln_Type=NT_MESSAGE;
  73.         msg.lcd_Message.mn_Length=sizeof(struct lcdmessage);
  74.         msg.lcd_Message.mn_ReplyPort=replyport;
  75.     }else{
  76.         throw VomitX();
  77.     }
  78.     msg.lcd_Priority=LCDPRI_APPLICATION;
  79.     msg.lcd_Code=LCDMSG_ASKPARAMS;
  80.     msg.lcd_Data=&lcdpar;
  81.     if(SendLCDMsg()){
  82.         WaitLCDMsg();
  83.     }else{
  84.         throw VomitX();
  85.     }
  86.  
  87. }
  88.  
  89. LCD::~LCD()
  90. {
  91.     if(messagepending)WaitLCDMsg();
  92.     if(replyport){
  93.         while(GetMsg(replyport));
  94.         DeleteMsgPort(replyport);
  95.     }
  96. }
  97.  
  98. void LCD::SetLCDMsg(LONG code,APTR data,LONG ticks,BYTE pri)
  99. {
  100.     msg.lcd_Code=code;
  101.     msg.lcd_Data=data;
  102.     msg.lcd_Priority=pri;
  103.     msg.lcd_Ticks=ticks;
  104. }
  105.  
  106. void LCD::SetLCDMsg(APTR data)
  107. {
  108.         msg.lcd_Data=data;
  109. }
  110.  
  111. int LCD::SendLCDMsg()
  112. {
  113.     struct MsgPort *port;
  114.     if(messagepending)return(FALSE);
  115.     Forbid();
  116.     port=FindPort(lcdportname);
  117.     if(port) {
  118.         PutMsg(port,(struct Message *)&msg);
  119.         messagepending=TRUE;
  120.     }
  121.     Permit();
  122.     return(port?TRUE:FALSE);
  123. }
  124.  
  125. int LCD::WaitLCDMsg(void)
  126. {
  127.     if(!messagepending)return(LCDERR_YOURFAULT);
  128.     WaitPort(replyport);
  129.     GetMsg(replyport);
  130.     messagepending=FALSE;
  131.     return(msg.lcd_Error);
  132. }
  133.  
  134. APTR LCD::LCDData(){
  135.     return(msg.lcd_Data);
  136. }
  137.  
  138. class LCDscreen : public LCD{
  139.     public:
  140.         LCDscreen();
  141.         void print(char *text);
  142.         void update();
  143.         UBYTE *character(int x,int y);
  144.         ~LCDscreen();
  145.     private:
  146.         struct lcdscreen *scr;
  147.         CustomCharDefs chars;
  148. };
  149.  
  150. UBYTE *LCDscreen::character(int x,int y)
  151. {
  152.     if(x>=width())x=width()-1;
  153.     if(y>=height())y=height()-1;
  154.     return &(scr->screenbuffer[y*scr->bufferwidth+x]);
  155. }
  156.  
  157. void LCDscreen::update()
  158. {
  159.     SetLCDMsg(LCDMSG_UPDATEHANDLE,scr,55,LCDPRI_MEMDISPLAY);
  160.     scr->ud_flags=LCDUPD_DISPLAY;
  161.     if(SendLCDMsg())WaitLCDMsg();
  162. }
  163.  
  164. void LCDscreen::print(char *text)
  165. {
  166.     strcpy(scr->screenbuffer,text);
  167.     SetLCDMsg(LCDMSG_UPDATEHANDLE,scr,100,LCDPRI_APPLICATION);
  168.     scr->ud_flags=LCDUPD_DISPLAY;
  169.     if(SendLCDMsg())WaitLCDMsg();
  170. }
  171.  
  172. LCDscreen::LCDscreen()
  173. {
  174.     scr=NULL;
  175.     SetLCDMsg(LCDMSG_ALLOCATEHANDLE,NULL,0,LCDPRI_MEMDISPLAY);
  176.     if(SendLCDMsg()){
  177.         if(!WaitLCDMsg()){
  178.             scr=(struct lcdscreen *)LCDData();
  179.             scr->customchardefs=chars;
  180.             scr->ud_flags=LCDUPD_CUSTOMCHARNUM|LCDUPD_CUSTOMCHARDEFS;
  181.             scr->customcharnum=6;
  182.             scr->customcharheight=8;
  183.             SetLCDMsg(LCDMSG_UPDATEHANDLE,scr,55,LCDPRI_MEMDISPLAY);
  184.             if(SendLCDMsg()){
  185.                 if(!WaitLCDMsg()){
  186.                     return;
  187.                 }
  188.             }
  189.         }
  190.     }
  191.     throw VomitX();
  192. }
  193.  
  194. LCDscreen::~LCDscreen()
  195. {
  196.     if(scr){
  197.         SetLCDMsg(LCDMSG_FREEHANDLE,scr,0,LCDPRI_MEMDISPLAY);
  198.         if(SendLCDMsg()){
  199.             WaitLCDMsg();
  200.         }
  201.     }
  202. }
  203.  
  204. class SysMon
  205. {
  206.     public:
  207.         SysMon(ArgsC &args) throw(VomitX);
  208.         ~SysMon();
  209.         int run(void) throw(VomitX);
  210.     private:
  211.         LCDscreen lcd;
  212.         struct SI_Notify *notify;
  213.         struct SI_CpuUsage use;
  214. };
  215.  
  216. SysMon::SysMon(ArgsC &args)
  217. {
  218.     if(!(notify=AddNotify(sysinfo,AN_USE_MESSAGES,10)))throw VomitX();
  219. }
  220.  
  221. int SysMon::run(void)
  222. {
  223.     ULONG ichipavail,ifastavail;
  224.     ULONG sigs=0,waitsig=SIGBREAKF_CTRL_C|(1L<<notify->notify_port->mp_SigBit);
  225.     struct Message *msg;
  226.     int chipwidth,fastwidth;
  227.     UBYTE theday[LEN_DATSTRING],thedate[LEN_DATSTRING],thetime[LEN_DATSTRING];
  228.     UBYTE schipavail[16],sfastavail[16];
  229.     char *c;
  230.     int daylen,datelen,timelen;
  231.     struct DateTime dt={{0,0,0},FORMAT_DOS,0,theday,thedate,thetime};
  232.     while(!(sigs&SIGBREAKF_CTRL_C)){
  233.         sigs=Wait(waitsig);
  234.         while(msg=GetMsg(notify->notify_port))ReplyMsg(msg);
  235.         GetCpuUsage(sysinfo,&use);
  236.         if(use.used_cputime_lastsec_hz){
  237.             int usage=5*lcd.width()*use.used_cputime_lastsec/use.used_cputime_lastsec_hz;
  238.             //lcd.print("Hello!");
  239.  
  240.             // Put chip & fast mem stats on line 0
  241.             ichipavail=AvailMem(MEMF_CHIP);
  242.             ifastavail=AvailMem(MEMF_FAST);
  243.             sprintf(schipavail,"C:%dK",ichipavail/1024);
  244.             sprintf(sfastavail,"F:%dK",ifastavail/1024);
  245.             memset(lcd.character(0,0),0,lcd.width()*lcd.height());
  246.             chipwidth=strlen(schipavail);
  247.             fastwidth=strlen(sfastavail);
  248.             c=lcd.character(lcd.width()-fastwidth,0);
  249.             strncpy(c,sfastavail,fastwidth);
  250.             c-=1+chipwidth;
  251.             strncpy(c,schipavail,chipwidth);
  252.             // Put date and time on line 1
  253.             if(lcd.height()>1){
  254.                 c=lcd.character(0,1);
  255.                 DateStamp(&dt.dat_Stamp);
  256.                 DateToStr(&dt);
  257.                 daylen=strlen(theday);datelen=strlen(thedate);timelen=strlen(thetime);
  258.                 if(daylen+datelen+timelen+2<=lcd.width()){
  259.                     sprintf(c,"%s %s %s",theday,thedate,thetime);
  260.                 }else if(datelen+timelen+1<=lcd.width()){
  261.                     sprintf(c,"%s %s",thedate,thetime);
  262.                 }else if(timelen<=lcd.width()){
  263.                     sprintf(c,"%s",thetime);
  264.                 }
  265.             }
  266.             // Overwrite with CPU load bar
  267.             c=lcd.character(0,0);
  268.             for(int i=usage/5;i;i--){
  269.                 *c++=5;
  270.             }
  271.             if(usage%5)*c++=usage%5;
  272.             lcd.update();
  273.         }
  274.     }
  275.     return(0L);
  276. }
  277.  
  278. SysMon::~SysMon()
  279. {
  280.     if(notify)RemoveNotify(sysinfo,notify);
  281. }
  282.  
  283. int main(void)
  284. {
  285.     int retval=5;
  286.     if(LibraryBaseC::areAllOpen()){
  287.         if(sysinfo=InitSysInfo()){
  288.             if(sysinfo->notify_msg_implemented && (sysinfo->cpu_usage_implemented&CPU_USAGEF_LASTSEC_IMPLEMENTED)){
  289.                 try{
  290.                     //ArgsC cmdline(NULL);
  291.                     SysMon theMon(NULL);
  292.                     retval=theMon.run();
  293.                 }catch(RdArgsX){
  294.                     cout<<"Invalid arguments"<<endl;
  295.                 }catch(...){
  296.                     cout<<"General failure. (C)rash,(F)ail: F"<<endl;
  297.                 }
  298.             }else{
  299.                 cout<<"No signal notification implemented"<<endl;
  300.             }
  301.             FreeSysInfo(sysinfo);
  302.         }
  303.     }
  304.     return(retval);
  305. }
  306.  
  307.